/**
 * 阿里云 top.oss 图片上传
 */
top.oss = {
    url:'',
    accessId:'',
    host :'',
    policyBase64 :'',
    signature :'',
    callback :'',
    filename :'',
    key : '',
    expire : 0,
    g_object_name : '',
    g_object_name_type : '',
    now : timestamp = Date.parse(new Date()) / 1000,
    urls : {
        getSignature:window.location.protocol  + "//" + '10.156.0.208:8080' + "/ks_manager/" + "manager/getSignature.do"
    },
    getSignature:function (){
        $.ajax({
            url: top.oss.urls.getSignature,
            //async:false,
            crossDomain:true,
            xhrFields:{
                withCredentials: true
            },
            success:function(data){
                data = JSON.parse(data)
                if(data.type == 1){
                    var content = data.content;
                    top.oss.host = content.host;
                    top.oss.policyBase64 = content.policy;
                    top.oss.accessId = content.accessId;
                        top.oss.signature = content.signature;
                    top.oss.expire = content.expire;
                    top.oss.callback = content.callback;
                    top.oss.key = content.dir;
                }
            }
        })
    },
    get_signature:function ()
    {
        //可以判断当前expire是否超过了当前时间,如果超过了当前时间,就重新取一下.3s 做为缓冲
        now = timestamp = Date.parse(new Date()) / 1000;
        if (top.oss.expire < now + 3)
        {
            top.oss.getSignature();
            return true;
        }
        return false;
    },
    getOssParam:function ( uploader ) {
        top.oss.get_signature()
        var g_object_name = top.oss.key;
        var new_multipart_params = {
            'key': g_object_name,
            'policy': top.oss.policyBase64,
            'OSSAccessKeyId': top.oss.accessId,
            'success_action_status': '200', //让服务端返回200,不然，默认会返回204
            'callback': top.oss.callback,
            'signature': top.oss.signature
        };
        /*
         if( data ){
         data['key'] = g_object_name;
         data['policy']= top.oss.policyBase64;
         data['OSSAccessKeyId']= top.oss.accessId;
         data['success_action_status'] = '200'; //让服务端返回200,不然，默认会返回204
         data['callback'] = top.oss.callback;
         data['signature']= top.oss.signature;
         }
         */
        if ( uploader) {
            if( !uploader.options.formData ) uploader.options.formData = {};
            uploader.options.formData['key'] = g_object_name;
            uploader.options.formData['policy'] = top.oss.policyBase64;
            uploader.options.formData['OSSAccessKeyId'] = top.oss.accessId;
            uploader.options.formData['success_action_status'] = '200'; //让服务端返回200,不然，默认会返回204
            uploader.options.formData['callback'] = top.oss.callback;
            uploader.options.formData['signature'] = top.oss.signature;
        }
        return new_multipart_params;
    },
    //post方法未测试，改的ueditor里面的initUploadBtn
    post:function( form  , iframe  , me , domUtils , showErrorLoader ){
        var formData = new FormData();
        top.oss.get_signature();
        formData.append('key' , g_object_name);
        formData.append('policy', top.oss.policyBase64);
        formData.append('OSSAccessKeyId', top.oss.accessId);
        formData.append('success_action_status' , '200'); //让服务端返回200,不然，默认会返回204
        formData.append('callback' , top.oss.callback);
        formData.append('signature', top.oss.signature);
        formData.append("upfile", form[0].files[0] );
        $.ajax({
            url: top.oss.host,
            type: 'POST',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            success: function (data) {
                data = JSON.parse(data);
                var link, loader,
                    body = (iframe.contentDocument || iframe.contentWindow.document).body,
                    result = body.innerText || body.textContent || '';
                link = me.options.imageUrlPrefix + data.url;

                if(data.state == 'SUCCESS' && data.url) {
                    loader = me.document.getElementById(loadingId);
                    loader.setAttribute('src', link);
                    loader.setAttribute('_src', link);
                    loader.setAttribute('title', data.title || '');
                    loader.setAttribute('alt', data.original || '');
                    loader.removeAttribute('id');
                    domUtils.removeClasses(loader, 'loadingclass');
                } else {
                    showErrorLoader && showErrorLoader(data.state);
                }
                form.reset();
            }
        });
    }
};






